import java.util.PriorityQueue;

/**
 * @author LKQ
 * @date 2022/4/10 15:38
 * @description 思路：逆向思维，每次找数组中最大值，然后左右扩散查找是否有紧挨着的最大值下标，然后统一减少1，重复
 * 毫无疑问，超时啦
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int minNumberOperations(int[] target) {
        int n = target.length;
        int ans = 0;
        do {
            int[] max = max(target);
            int value = max[0], index = max[1];
            int left = index, right = index;
            while (right + 1 < n && target[right + 1] == value) {
                right++;
            }
            while (left - 1 >= 0 && target[left - 1] == value) {
                left--;
            }
            diff(target, left, right);
            ans++;
        } while (!allZero(target));
        return ans;
    }
    public int[] max(int[] target) {
        int ans = 0;
        int index = 0;
        for (int i = 0; i < target.length; i++) {
            if (target[i] > ans) {
                ans = target[i];
                index = i;
            }
        }
        return new int[]{ans, index};
    }
    public void diff(int[] target, int left, int right) {
        for (int i = left; i <= right; i++) {
            target[i]--;
        }
    }
    public boolean allZero(int[] target) {
        for (int j : target) {
            if (j != 0) {
                return false;
            }
        }
        return true;
    }
}
